package com.gopay.common.rcs.manager.antiphish.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gopay.common.constants.rcs.AntiPhishCheckOrderIpEnum;
import com.gopay.common.constants.rcs.AntiPhishEditNatureEnum;
import com.gopay.common.constants.rcs.AntiPhishGetwayChannelEnum;
import com.gopay.common.constants.trans.GatewayChannel;
import com.gopay.common.domain.rcs.RcsAntiPhishConfCust;
import com.gopay.common.domain.rcs.RcsAntiPhishConfEditLog;
import com.gopay.common.rcs.bean.AntiPhishType;
import com.gopay.common.rcs.dao.antiphish.RcsAntiPhishEditLogDAO;
import com.gopay.common.rcs.dao.antiphish.RcsAntiPhishingCustDAO;
import com.gopay.common.rcs.manager.antiphish.RcsAntiPhishingCustManager;
import com.gopay.common.rcs.vo.RcsAntiPhishConfCustlVo;
import com.gopay.common.util.StringTools;

/**
 * 防钓鱼商户定制配置
 * 
 * @RcsAntiPhishingCustManager.java
 * @author liujy
 * @2013-7-23 下午6:35:31 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("rcsAntiPhishingCustManager")
public class RcsAntiPhishingCustManagerImpl implements RcsAntiPhishingCustManager {

    @Resource(name = "rcsAntiPhishingCustDAO")
    RcsAntiPhishingCustDAO rcsAntiPhishingCustDAO;
    
    @Resource(name = "rcsAntiPhishEditLogDAO")
    RcsAntiPhishEditLogDAO rcsAntiPhishEditLogDAO;

    /**
     * 初次设置单个商户的 所有验证规则
     * 
     * @param custConfs
     */
    @Override
    @Transactional
    public void saveCustConf(RcsAntiPhishConfCustlVo newCustVo,List<RcsAntiPhishConfCust> custConfs,String operId,String operName) {
        for (RcsAntiPhishConfCust custConf : custConfs) {
            rcsAntiPhishingCustDAO.save(custConf);
            //记日志
            if(custConf.getAntiPhishType().equals("01")){// 01 - 精确域名校验
            	 //记日志 开关
                RcsAntiPhishConfEditLog log=getBaseLog(operId, operName, custConf.getConfSq());
                log.setValBeforeEdit("");
                log.setValAfterEdit(newCustVo.getCheckDomain().equals("1")?"开启":"关闭");
                log.setEditNature(AntiPhishEditNatureEnum.ANTI_PHISH_TYPE.value);
                rcsAntiPhishEditLogDAO.save(log);
                if(!custConf.getLegalValue().equals("")){
                    //记日志域名列表
                    RcsAntiPhishConfEditLog log_=getBaseLog(operId, operName, custConf.getConfSq());
                    log_.setValBeforeEdit("");
                    log_.setValAfterEdit(custConf.getLegalValue());
                    log_.setEditNature(AntiPhishEditNatureEnum.LEGAL_VALUE.value);
                    rcsAntiPhishEditLogDAO.save(log_);
                }
            }else if( custConf.getAntiPhishType().equals("02")){// 02 - 域名模糊校验
                        //记日志
                    RcsAntiPhishConfEditLog log=getBaseLog(operId, operName, custConf.getConfSq());
                    log.setValBeforeEdit("");
                    log.setValAfterEdit(newCustVo.getCheckLikeDomain().equals("1")?"开启":"关闭");
                    log.setEditNature(AntiPhishEditNatureEnum.ANTI_PHISH_TYPE.value);
                    rcsAntiPhishEditLogDAO.save(log);
            }else if(custConf.getAntiPhishType().equals("03")){// 03 - 时间戳间隔校验
                    //记日志 开关
                    RcsAntiPhishConfEditLog log=getBaseLog(operId, operName, custConf.getConfSq());
                    log.setValBeforeEdit("");
                    log.setValAfterEdit(newCustVo.getCheckTimeStamp().equals("1")?"开启":"关闭");
                    log.setEditNature(AntiPhishEditNatureEnum.DISJUNCTOR.value);
                    rcsAntiPhishEditLogDAO.save(log);
                //单商户时间错合法值不能更改
                
            }else if(custConf.getAntiPhishType().equals("04")){// 04 - 下单及支付IP比对校验
                if(!custConf.getIllegalReqDicision().equals(AntiPhishCheckOrderIpEnum.CLAOSE.value)){
                    //记日志
                    RcsAntiPhishConfEditLog log=getBaseLog(operId, operName, custConf.getConfSq());
                    log.setValBeforeEdit("");
                    log.setValAfterEdit(getCheckOrderIPName(newCustVo.getCheckOrderIP()));
                    log.setEditNature(AntiPhishEditNatureEnum.ILLEGAL_REQ_DICISION.value);
                    rcsAntiPhishEditLogDAO.save(log);
                }
            }else if(custConf.getAntiPhishType().equals("05")){// 05 - 验证码提示页
                if(!custConf.getGatewayChannel().equals(AntiPhishGetwayChannelEnum.ALL_CLOSE.value)){
                    //记日志
                    RcsAntiPhishConfEditLog log=getBaseLog(operId, operName, custConf.getConfSq());
                    log.setValBeforeEdit("");
                    log.setValAfterEdit(getEnterValidatinName(newCustVo.getEnterValidityCode()));
                    log.setEditNature(AntiPhishEditNatureEnum.GATEWAY_CHANNEL.value);
                    rcsAntiPhishEditLogDAO.save(log);
                }
                if(custConf.getLegalValue()!=null&&!custConf.getLegalValue().equals("")){
                    //上传logo记日志
                    RcsAntiPhishConfEditLog log=getBaseLog(operId, operName, custConf.getConfSq());
                    log.setValBeforeEdit("");
                    log.setValAfterEdit(newCustVo.getUploadLogo());
                    log.setEditNature(AntiPhishEditNatureEnum.LEGAL_VALUE.value);
                    rcsAntiPhishEditLogDAO.save(log);
                }
            }else if(custConf.getAntiPhishType().equals("06")){// 06 - 交易请求源IP校验
                if(!custConf.getLegalValue().equals("")){
                    //记日志
                    RcsAntiPhishConfEditLog log=getBaseLog(operId, operName, custConf.getConfSq());
                    log.setValBeforeEdit("");
                    log.setValAfterEdit(StringTools.checkNulltoString(newCustVo.getReqSourceIp()));
                    log.setEditNature(AntiPhishEditNatureEnum.LEGAL_VALUE.value);
                    rcsAntiPhishEditLogDAO.save(log);
                }
            }
        }
    }
    
    private String getEnterValidatinName(String enterValidatiCode){
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.ALL_CLOSE.value)){
            return "关闭";
        }
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.DIRECT_BANK.value)){
            return "直连开启";
        }
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.UN_DIRECT_BANK.value)){
            return "非直连开启";
        }
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.ALL_OPEN.value)){
            return "全部开启";
        }
        return null;
    }
    
    private String getCheckOrderIPName(String checkOrderIP){
        if(checkOrderIP.equals(AntiPhishCheckOrderIpEnum.CLAOSE.value)){
            return "关闭";
        }else if(checkOrderIP.equals(AntiPhishCheckOrderIpEnum.REJECT.value)){
            return "拒绝支付";
        }else if(checkOrderIP.equals(AntiPhishCheckOrderIpEnum.WARN.value)){
            return "提示用户";
        }
        return null;
    }
    
    /**
     * 
     * log基础数据
     * @param operId
     * @param operName
     * @param confSq
     * @return
     */
    private RcsAntiPhishConfEditLog getBaseLog(String operId,String operName,int confSq){
        RcsAntiPhishConfEditLog log=new RcsAntiPhishConfEditLog();
        log.setOperConfType("02");
        log.setOperId(operId);
        log.setOperName(operName);
        log.setOperTargetSq(confSq+"");
        return log;
        
    }
    private String  gatewayChannelName(String gatewayChannel){
        if(gatewayChannel.equals(AntiPhishGetwayChannelEnum.ALL_OPEN.value)){
           return "全部开启";
        }
        if(gatewayChannel.equals(AntiPhishGetwayChannelEnum.DIRECT_BANK.value)){
            return "直连开启";
        }
        if(gatewayChannel.equals(AntiPhishGetwayChannelEnum.UN_DIRECT_BANK.value)){
            return "非直连开启";
        }
        return "";
    }

    /**
     * 商户一次修改操作涉及多条 验证条件 需要在一个事物中
     * 
     * @param custConfs
     */
    @Override
    @Transactional
    public void updateCustConf(List<RcsAntiPhishConfCust> custConfs, List< RcsAntiPhishConfEditLog> logList) {
        for (RcsAntiPhishConfCust custConf : custConfs) {
            rcsAntiPhishingCustDAO.updateCustConf(custConf);
        }
        //记录日志
        for(RcsAntiPhishConfEditLog log:logList){
            rcsAntiPhishEditLogDAO.save(log);
        }
    }

    @Override
    public List<?> getCustConfBySqlList(String custName, String email, String custMob, int pageNo, int pageSize) {
        return rcsAntiPhishingCustDAO.getCustConfListBySql(custName, email, custMob, pageNo, pageSize);
    }

    @Override
    public int getCount(String custName, String email, String custMob) {
        return rcsAntiPhishingCustDAO.getCount(custName, email, custMob);
    }

    @Override
    public List<?> getCustConfBySql(String custId) {
        return rcsAntiPhishingCustDAO.getCustConfBySql(custId);
    }

    @Override
    public List<RcsAntiPhishConfCustlVo> getMerchantBySql(String custName, String email, String custMob, int pageNo, int pageSize) {
        return rcsAntiPhishingCustDAO.getMerchantBySql(custName, email, custMob, pageNo, pageSize);
    }

    @Override
    public int getMerchantCount(String custName, String custEmail, String custMob) {
        return rcsAntiPhishingCustDAO.getMerchantCount(custName, custEmail, custMob);
    }

	@Override
	public RcsAntiPhishConfCustlVo getMerchantByCustId(String custId) {
		return rcsAntiPhishingCustDAO.getMerchantByCustId(custId);
	}
    
    @Override
    public String getLegalValue(String custId, AntiPhishType type, GatewayChannel gch) {
        return rcsAntiPhishingCustDAO.getLegalValue(custId, type, gch);
    }

    @Override
    public RcsAntiPhishConfCust getByCustId(String custId) {
        return rcsAntiPhishingCustDAO.getByCustId(custId); 
    }   

    /**
     * 删除商户logo
     * @param merchantId
     * @param operId
     * @param operName
     * @param ConfSq 关联
     * @param beforeEditUrl
     */
    @Override
    public void delLogo(String custId,String operId,String operName) {
        
        //上传logo记日志
        RcsAntiPhishConfCust rcsAntiPhishConfCust= rcsAntiPhishingCustDAO.getByCustId(custId);
        RcsAntiPhishConfEditLog log=new RcsAntiPhishConfEditLog();
        log.setOperConfType("02");
        log.setOperId(operId);
        log.setOperName(operName);
        log.setOperTargetSq(rcsAntiPhishConfCust.getConfSq()+"");
        log.setValBeforeEdit(rcsAntiPhishConfCust.getLegalValue());
        log.setValAfterEdit("");
        log.setEditNature(AntiPhishEditNatureEnum.LEGAL_VALUE.value);
        rcsAntiPhishEditLogDAO.save(log);
        rcsAntiPhishingCustDAO.delLogo(custId);
        
        
    }

}
